import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2023-01-23
 * Time:21:32
 */
class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode() {

    }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode next = head;
        int flag = 0;
        while (l1 != null && l2 != null) {
            ListNode node = new ListNode();
            next.next = node;
            next = node;
            node.val = l1.val + l2.val + flag;
            flag = 0;
            if (node.val > 9) {
                node.val = node.val - 10;
                flag = 1;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            l1.val += flag;
            flag = 0;
            if (l1.val > 9) {
                l1.val = l1.val - 10;
                flag = 1;
            }
            next.next = l1;
            next = l1;
            l1 = l1.next;
        }
        while (l2 != null) {
            l2.val += flag;
            flag = 0;
            if (l2.val > 9) {
                l2.val = l2.val - 10;
                flag = 1;
            }
            next.next = l2;
            next = l2;
            l2 = l2.next;
        }
        if (flag == 1) {
            ListNode node = new ListNode(1);
            next.next = node;
        }
        return head.next;
    }

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        int[] arr = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (arr[i] != 1) {
                int j = 1;
                while (j <= k && j + i < nums.length) {
                    if (nums[i] == nums[i + j]) {
                        arr[i] = 1;
                        arr[i + j] = 1;
                    }
                    j++;
                }
            }
        }
        boolean flag = false;
        for (int i = 0; i < nums.length; i++) {
            if (arr[i] == 1) {
                flag = true;
            }
        }
        return flag;
    }
    public boolean containsNearbyDuplicate1(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; ++i){
            if(map.containsKey(nums[i])){
                if((i - map.get(nums[i]) <= k)) return true;
            }
            // 覆盖之后的位置和下一个相同值肯定离得更近
            map.put(nums[i], i);
        }
        return false;
    }
}

public class Test {
    public static void main(String[] args) {
//        ListNode l1=new ListNode(1);
//        ListNode l2=new ListNode(9);
        Solution solution = new Solution();
//        ListNode node=solution.addTwoNumbers(l1,l2);
//        while(node!=null){
//            System.out.println(node.val);
//            node=node.next;
//        }
        int arr[] = {1, 2, 3, 1};
        System.out.println(solution.containsNearbyDuplicate(arr, 3));

    }
}
